package com.example.springbootdemo.web;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class MailAction {

    @Value("${myEmail}")
    String from;

    // 前提 : spring-boot-starter-mail + spring.mail.host
    @Resource
    JavaMailSender mailSender;

    @RequestMapping("mailTo")
    public String sendEmail(String to, String title, String content){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject(title);
        message.setFrom(from);
        message.setTo(to);
        message.setText(content);
        mailSender.send(message);
        return "邮件发送成功!";
    }

}
